import { loginApi, coachLogoutApi } from '@/api/api'

const user = {
  state: {
    userObj: {}
  },
  actions: {
    Login({commit}, data) {
      let myApi = ''
      if(data.userType == '2') {
        myApi = loginApi(data.userInfo)
      } else {
        myApi = coachLogoutApi(data.userInfo)
      }
      return new Promise((resolve, reject) => {
        myApi.then(res => {
          const data = res.data;
          if (res.code !== 200) {
            Message({
              message: res.message,
              type: 'error'
            })
          } else {
            commit('SET_USER_INFO', data);
          }
          resolve(data);
        }).catch(error => {
          reject(error);
        })
      })
    }
  },
  mutations: {
    SET_USER_INFO(state, user) {
      state.userObj = user;
    },
  }

};

export default user;
